<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>登录页面</title>
    <style>
        .loading {
            display: none;
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: rgba(0, 0, 0, 0.7);
            color: white;
            padding: 20px;
            border-radius: 5px;
        }
        .error-message {
            color: red;
            margin-top: 10px;
        }
    </style>
</head>
<body>
    <div>
        <h2>登录</h2>
            <label for="username">用户名:</label>
            <input type="text" id="username" name="username" required><br>
            <label for="password">密码:</label>
            <input type="password" id="password" name="password" required><br>
            <button onclick="login()">登录</button>
        <a href="/register-login">Register</a>
        <div id="errorMessage" class="error-message"></div>
        <div id="loading" class="loading">
            <div class="spinner"></div>
            登录中...
        </div>
    </div>

    <script>

        function login() {
            // 显示加载提示
            document.getElementById('loading').style.display = 'block';
            document.getElementById('errorMessage').textContent = '';



            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            fetch('/authLogin', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    username: username,
                    password: password
                })
            })
            .then(response => response.json())
            .then(data => {
                // 隐藏加载提示
                document.getElementById('loading').style.display = 'none';
                console.log(data);
                if (data.success) {
                    // 登录成功，跳转到指定页面
                    const token = data.token;
					console.log(token);
                    localStorage.setItem('token', token);
                    // 手动触发携带 Authorization 头的页面跳转
                    const xhr = new XMLHttpRequest();
                    xhr.open('GET', data.redirectUrl, true);
                    xhr.setRequestHeader('Authorization', `Bearer ${token}`);
                    xhr.onreadystatechange = function() {
                        if (xhr.readyState === 4) {
                            if (xhr.status === 200) {
                                // 替换当前页面内容
                                document.documentElement.innerHTML = xhr.responseText;
                            } else {
                                document.getElementById('errorMessage').textContent = '跳转页面请求失败，请稍后重试';
                            }
                        }
                    };
                    xhr.send();
                } else {
                    document.getElementById('errorMessage').textContent = data.message;
                }
            })
           .catch(error => {
                // 隐藏加载提示
                document.getElementById('loading').style.display = 'none';
                document.getElementById('errorMessage').textContent = '登录请求出错，请稍后重试';
            });
        }

        function adminPage() {
			console.log("跳转管理员页面");
            const token = localStorage.getItem('token');
            // 手动触发携带 Authorization 头的页面跳转
            const xhr = new XMLHttpRequest();
            xhr.open('GET', '/admin', true);
            xhr.setRequestHeader('Authorization', `Bearer ${token}`);
            xhr.onreadystatechange = function() {
				console.log(xhr);
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        // 替换当前页面内容
                        document.documentElement.innerHTML = xhr.responseText;
                    } else {
                        document.getElementById('errorMessage').textContent = '跳转页面请求失败，请稍后重试';
                    }
                }
            };
            xhr.send();
        }
    </script>
</body>
</html>
